/*eslint-disable*/
import nowTime from './nowTime';


type Todolist={
    [date:string]:string
}

const useTodolist=()=>{
    const getTodolist =()=>{
        let a:Array<Todolist> = JSON.parse(localStorage.getItem('todolist')||'[]')
        return a
    }
    const addTodolist=(val:string|undefined)=>{
        if(val===undefined){
            return
        }else{
            let a = getTodolist()
            let date = nowTime()
            a.push({[date]:val})
            localStorage.setItem('todolist',JSON.stringify(a))
            window.alert('添加成功')
        }
        
    }
    const setTodolist=(date:string,oldVal:string,newVal:string)=>{
        let a ={}
        a[date]=oldVal
        let oldTodolist = getTodolist()
        oldTodolist.map((items)=>{
            if(items[date]===a[date]){
                items[date]= newVal
            }
        })
        localStorage.setItem('todolist',JSON.stringify(oldTodolist))
    }
    let rmTodolist =(index:number)=>{
        let a =getTodolist()
        a.splice(index,1)
        localStorage.setItem('todolist',JSON.stringify(a))
        window.alert('删除成功')
    }

    return{addTodolist,getTodolist,setTodolist,rmTodolist}
}

export default useTodolist